<?php

class indexAction extends sfAction
{
    public function execute($request)
    {
        if (App::getCurrentApiId() !== App::getMainApiId()) {
            $this->getUser()->setAttribute('fromHomePage', true);
            $this->forward('redirect', 'getStarted');
        }

        if (!$this->getUser()->isAuthenticated()) {
            $this->getUser()->setAttribute('fromHomePage', true);
            $this->forward('redirect', 'getStarted');
        }

        $this->pages = PageListManager::getInstalledPagesArray();
        $this->is_customer = $this->getUser()->getFbUser()->isCustomer();

        // users who have 1 page with Work for Us installed are redirected to backoffice_list
        if (count($this->pages) === 1 && !$this->is_candidate)
            $this->redirect('backoffice/list?page_id='.key($this->pages));

        // redirection if no Page with work for us and no CandidateDataset
        if (!$this->pages && !$this->is_candidate) {
            $this->getUser()->setAttribute('fromHomePage', true);
            $this->forward('redirect', 'getStarted');
        // if no Pages && CandidateDataset
        } elseif (!$this->pages && $this->is_candidate) {
            $this->redirect('@candidate_base');
        } else {
            return sfView::SUCCESS;
        }
    }
}

